class PhotoController < ApplicationController
  before_filter :protect, :vip?
  before_filter :protect_photo, :only => [ :editpic, :deletepic ]
  layout 'user'
  
  def new
    @title = "形象库"
    if param_posted?(:photo)
      @photo = Photo.new(params[:photo]) 
      if @login_user.photos<<@photo
          redirect_to :action => :showpics
      end
   end
  end

  def showpics
    @title = "形象库"
    @photos = @login_user.photos
  end

  def showdetail
    @title = "形象库"
    @photo = Photo.find(params[:id])
  end
  
  def editpic
    @title = "形象库"
    @photo = Photo.find(params[:id])
    if param_posted?(:photo)
      if @photo.update_attributes(params[:photo])
        redirect_to :action => :showpics
      end  
    end
  end
  
  def deletepic
    photo = Photo.find(params[:id])
    photo.update_attribute(:pic,nil)
    @login_user.photos.delete(photo)
    redirect_to :action => :showpics
  end


private

 def protect_photo
   @photo = Photo.find(params[:id])
   unless @photo.user == @login_user
     flash[:notice] = "这不是您的形象库"
     redirect_to profile_for(@login_user)
   end
 end
  
 def vip?
    unless @login_user.is_vip == "Yes"
      role = @login_user.role == DESIGNER ? "创意宝" : "创意寻"
      flash[:notice] = "您还不是#{role}vip会员，请升级到#{role}"
      redirect_to :controller => "admin/profile", :action => "vip_register"
    end
  end

end
